package org.ksug.samples.springmvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

@Controller
public class AccountFormController extends SimpleFormController {

	@Autowired
	private AccountRepository repository;

	public AccountFormController() {
		this.setFormView("accounts/add");
		this.setCommandClass(Account.class);
	}

	@Override
	protected ModelAndView onSubmit(Object command) throws Exception {
		Account account = (Account) command;
		repository.add(account);
		return new ModelAndView("redirect:/accounts/show.do?number="
				+ account.getNumber());
	}

	public void setRepository(AccountRepository repository) {
		this.repository = repository;
	}
}
